package com.wyj.longrangeclient.Service;

import android.accessibilityservice.AccessibilityService;
import android.accessibilityservice.GestureDescription;
import android.content.Context;
import android.content.Intent;
import android.graphics.Path;
import android.os.PowerManager;
import android.view.accessibility.AccessibilityEvent;

import com.wyj.longrangeclient.Bean.ActionBean;
import com.wyj.longrangeclient.Util.RxBus;

import rx.Observer;

/**
 * author : 泥菩萨
 * time   : 2023/04/18
 */
public class YourAccessibilityService extends AccessibilityService {

    @Override
    protected void onServiceConnected() {
        super.onServiceConnected();

        //用来让service不进入闲置 避免被回收
        RxBus.getInstance().Register("AccessibilityService", new Observer<Object>() {

            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(Object o) {

                RxBus.getInstance().post("ServiceMsg","无障碍服务:正在运行");
            }
        });

        RxBus.getInstance().Register("YourAccessibilityService", new Observer<Object>() {

            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(Object o) {

                try{

                    handle(o);

                }catch (Exception e){

                }
            }
        });
    }

    @Override
    public void onAccessibilityEvent(AccessibilityEvent accessibilityEvent) {

    }

    @Override
    public void onInterrupt() {

    }

    @Override
    public boolean onUnbind(Intent intent) {

        RxBus.getInstance().Cancellation("YourAccessibilityService");
        RxBus.getInstance().Cancellation("AccessibilityService");

        RxBus.getInstance().post("ServiceMsg","无障碍服务:关闭");

        return super.onUnbind(intent);
    }

    private void handle(Object data){

        ActionBean mActionBean = (ActionBean)data;

        if (mActionBean.getAppAction().equals("dj")){

            dj(mActionBean.getX1(),mActionBean.getY1());

        }else if (mActionBean.getAppAction().equals("ca")){

            ca(mActionBean.getX1(),mActionBean.getY1());

        }else if (mActionBean.getAppAction().equals("hd")){

            hd(mActionBean.getX1(),mActionBean.getY1(),mActionBean.getX2(),mActionBean.getY2());

        }else  if (mActionBean.getAppAction().equals("home")){

            performGlobalAction(GLOBAL_ACTION_HOME);

        }else  if (mActionBean.getAppAction().equals("back")){

            performGlobalAction(GLOBAL_ACTION_BACK);
        }
    }

    private void dj(float x, float y) {

        GestureDescription.Builder builder = new GestureDescription.Builder();

        Path path = new Path();
        path.moveTo(x, y);
        path.lineTo(x, y);
        builder.addStroke(new  GestureDescription.StrokeDescription(path, 0, 1));

        GestureDescription gesture = builder.build();

        dispatchGesture(gesture, new GestureResultCallback() {
            @Override
            public void onCancelled(GestureDescription gestureDescription) {
                super.onCancelled(gestureDescription);
            }

            @Override
            public void onCompleted(GestureDescription gestureDescription) {
                super.onCompleted(gestureDescription);
            }
        },null);
    }

    private void ca(float x, float y) {

        GestureDescription.Builder builder = new GestureDescription.Builder();

        Path path = new Path();
        path.moveTo(x, y);
        path.lineTo(x, y);
        builder.addStroke(new  GestureDescription.StrokeDescription(path, 0, 3000));

        GestureDescription gesture = builder.build();

        dispatchGesture(gesture, new GestureResultCallback() {
            @Override
            public void onCancelled(GestureDescription gestureDescription) {
                super.onCancelled(gestureDescription);
            }

            @Override
            public void onCompleted(GestureDescription gestureDescription) {
                super.onCompleted(gestureDescription);
            }
        },null);
    }

    private void hd(float x1,float y1,float x2,float y2){

        Path path = new Path();
        path.moveTo(x1, y1);//滑动起点
        path.lineTo(x2, y2);//滑动终点

        GestureDescription.Builder builder = new GestureDescription.Builder();
        GestureDescription gesture = builder.addStroke(new GestureDescription.StrokeDescription(path, 0L, 10L)).build();

        //100L 第一个是开始的时间，第二个是持续时间
        dispatchGesture(gesture,  new GestureResultCallback() {
            @Override
            public void onCancelled(GestureDescription gestureDescription) {
                super.onCancelled(gestureDescription);
            }

            @Override
            public void onCompleted(GestureDescription gestureDescription) {
                super.onCompleted(gestureDescription);
            }
        }, null);
    }

}
